package g

import (
	"github.com/Shopify/sarama"
	"github.com/globalsign/mgo"
	"github.com/gomodule/redigo/redis"
	"github.com/jinzhu/gorm"
	"github.com/shima-park/agollo"
	"github.com/spf13/viper"
	"lol.com/feb214-game/nest.git/cache"
	"lol.com/feb214-game/nest.git/mg"
	"lol.com/feb214-game/nest.git/user"
	"sync"
)

//导出全局变量
//理论上table最好按游戏类型分类，不过实际上线的时候肯定只激活一种游戏服务，所以就不分了
var (
	ServerCloseChan = make(chan bool)
	ServerCloseWG   = &sync.WaitGroup{}
	AllTables       = &sync.Map{}
	AllAgents       = &sync.Map{}
	ServerClosed    int32

	IMPool    *redis.Pool
	ORM       *gorm.DB
	MG        *mgo.Session
	RedisPool *redis.Pool
	StatsDAO  *mg.UserStatsDAO
	KafkaPool sarama.AsyncProducer
	Ap        agollo.Agollo

	publicCaches      = make(map[int32]*cache.PublicCache, 4)
	accountDAOes      = make(map[int32]*user.AccountDAO, 4)
	accountRobotDAOes = make(map[int32]*user.AccountRobotDAO, 4)
	bankerGames       = map[int32]bool{}

	// 配置中心
	Viper *viper.Viper
)

//以下函数非并发安全，仅在初始化的时候调用写一次
func SetPublicCache(gameType int32, publicCache *cache.PublicCache) {
	publicCaches[gameType] = publicCache
}

func GetPublicCache(gameType int32) *cache.PublicCache {
	return publicCaches[gameType]
}
func SetAccountDAO(gameType int32, accountDAO *user.AccountDAO) {
	accountDAOes[gameType] = accountDAO
}
func GetAccountDAO(gameType int32) *user.AccountDAO {
	return accountDAOes[gameType]
}

func SetAccountRobotDAO(gameType int32, accountDAO *user.AccountRobotDAO) {
	accountRobotDAOes[gameType] = accountDAO
}

func GetAccountRobotDAO(gameType int32) *user.AccountRobotDAO {
	return accountRobotDAOes[gameType]
}

func CleanLoginInfo() {
	for _, pc := range publicCaches {
		pc.CleanLoginInfo()
	}
}

//结算时可能输钱的游戏，目前百牛和炸金花
func IsAwardLoseGame(gameType int32) bool {
	return gameType == Jmg
}

//可以上庄的游戏
func IsBankerGame(gameType int32) bool {
	return bankerGames[gameType]
}
